<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/style.css">
    <style>
        form {
            width: 270px;
        }

        input {
            width: 64%;
            background: #eee;
        }

        input:focus {
            background: #fff;
        }

        form {
            padding: 0 12px 12px;
        }

        label {
            display: block;
            padding-bottom: 12px;
        }

        .label-text {
            width: 36%;
            float: left;
        }

        #img-show {
            width: 135px;
            height: 135px;
            display: block;
            margin: 0 auto;
            object-fit: cover;
        }
    </style>
    <script>
        function imgfileChange() {
            var img_show = document.getElementById("img-show");
            var imgfile = document.getElementById("imgfile");

            var freader = new FileReader();
            freader.readAsDataURL(imgfile.files[0]);
            freader.onload = function (e) {
                img_show.src = e.target.result;
            };
        }
    </script>
</head>
<body>
<div id="app">
    <div id="wrap">
        <div id="header">
            <div style="float: right;padding-top: 24px">2009/11/20</div>
            <h1>旅游信息管理系统</h1>
        </div>
        <div id="header-bar"></div>
        <div id="content" style="height: 440px">
            <img src="../img/timg.jpg" style="float: right;height: 320px">
            <h2>修改景点</h2>
            <form action="viewspotlist.html" method="post">
                <label>
                    <div class="label-text">景&emsp;&emsp;点：</div>
                    <input type="text" v-model="place.name">
                </label>
                <label>
                    <div class="label-text">印象图片：</div>
                    <div style="text-align: center;padding-left: 36%">
                        <img :src="src" alt="" id="img-show">
                        <input type="file" id="imgfile" ref="myFile" style="display: none" onchange="imgfileChange()">
                    </div>
                </label>
                <label>
                    <div class="label-text">旺季时间：</div>
                    <input type="text" v-model="place.hotTime">
                </label>
                <label>
                    <div class="label-text">旺季门票：</div>
                    <input type="text" v-model="place.hotTicket">
                </label>
                <label>
                    <div class="label-text">淡季门票：</div>
                    <input type="text" v-model="place.dimTicket">
                </label>
                <label>
                    <div class="label-text">景点描述：</div>
                    <input type="text" v-model="place.placeDes">
                </label>
                <label>
                    <div class="label-text">所属省份：</div>
                    <select v-model="place.provinceId">
                        <option v-for="pro in provinces" v-text="pro.name" :value="pro.id"></option>
                    </select>
                </label>
                <button type="button" @click="submit">提 交</button>&emsp;
                <a :href="'./viewspotlist.html?provinceId='+place.provinceId">返回</a>
            </form>
        </div>
        <div id="footer">
            ABC@126.com
        </div>
    </div>
</div>
</body>
</html>

<script src="../js/vue.js"></script>
<script src="../js/axios.min.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            id: "",
            place: {},
            provinces: [],
            src: "",
        },
        methods: {
            findOne() {
                _this = this;
                axios.get("http://localhost:8989/place/findOne?id=" + this.id).then((res) => {
                    _this.place = res.data;
                    _this.src = "data:image/jpeg;base64," + _this.place.picPath;
                    // console.log(_this.place);
                })
            },
            findAll() {
                _this = this;
                axios.get("http://localhost:8989/province/findAll").then((res) => {
                    _this.provinces = res.data.provinces
                });
            },
            submit() {
                let myFile = this.$refs.myFile;
                let files = myFile.files;
                let file = files[0];
                let formData = new FormData();
                formData.append("pic", file);
                formData.append("id",this.id);
                formData.append("name", this.place.name);
                formData.append("hotTime", this.place.hotTime);
                formData.append("hotTicket", this.place.hotTicket);
                formData.append("dimTicket", this.place.dimTicket);
                formData.append("placeDes", this.place.placeDes);
                formData.append("provinceId", this.place.provinceId);
                axios({
                    method: "POST",
                    url: 'http://localhost:8989/place/update',
                    data: formData,
                    headers: {
                        'Content-type': 'multipart/form-data',
                    }
                }).then((res) => {
                    console.log(res.data);
                    if (res.data.state) {
                        alert(res.data.msg + "点击确定回到省份")
                        location.href = "./viewspotlist.html?id=" + this.place.provinceId;
                    } else {
                        alert(res.data.msg);
                    }
                });
            }
        },
        created() {
            let id = location.href.substring(location.href.indexOf("=") + 1);
            this.id = id;
            this.findOne();
            this.findAll();
        }
    })
</script>